<template>
  <div>
        <el-dialog
      :title="obj.isAdd ? '分类添加' : '分类编辑'"
      :visible.sync="obj.isShow"
      width="50%"
       @opened="opened()"
    >
     <el-form>
     <el-form-item label="一级分类">
    <el-select v-model="form.first_cateid" placeholder="请选择一级分类" @change="OneCate()">
      <el-option v-for="item in CateList" :key="item.id" :label="item.catename" :value="item.id"></el-option>
    </el-select>
  </el-form-item>
   <el-form-item label="二级分类">
    <el-select v-model="form.second_cateid" placeholder="请选择二级分类">
      <el-option  v-for="item in secondCate" :key="item.id"   :label="item.catename" :value="item.id"></el-option>
    </el-select>
  </el-form-item>
    <el-form-item label="商品名称" label-width="80px">
    <el-input v-model="form.goodsname"></el-input>
  </el-form-item>
    <el-form-item label="价格" label-width="80px">
    <el-input  v-model="form.price"></el-input>
  </el-form-item>
    <el-form-item label="市场价格" label-width="80px">
    <el-input  v-model="form.market_price"></el-input>
  </el-form-item>
     <el-form-item label="图片" label-width="80px">
         <div class="box">
           <i>+</i>
           <input type="file" name="" id="" v-if="obj.isShow" @change="changeimg">
           <img :src="imgUrl" alt="" v-if="imgUrl" >
        </div>
  </el-form-item>
   <el-form-item label="商品规格">
    <el-select v-model="form.specsid" placeholder="请选择活动区域" @change="changespecs">
      <el-option  v-for="item in SpecsList " :key="item.id" :label="item.specsname" :value="item.id" ></el-option>
    </el-select>
  </el-form-item>
   <el-form-item label="商品属性">
    <el-select multiple v-model="form.specsattr" placeholder="请选择活动区域">
      <el-option  v-for="(item,index) in attrArr" :key="index" :label="item.value" :value="item"></el-option>
    </el-select>
  </el-form-item>
      <el-form-item label="是否新品">
    <el-radio-group v-model="form.isnew">
      <el-radio :label="1">是</el-radio>
      <el-radio :label="0">否</el-radio>
    </el-radio-group>
  </el-form-item>
    <el-form-item label="是否热卖">
    <el-radio-group v-model="form.ishot">
      <el-radio :label="1">是</el-radio>
      <el-radio :label="0">否</el-radio>
    </el-radio-group>
  </el-form-item>
    <el-form-item label="状态">
        <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
    </el-form-item>
    <el-form-item label="商品描述" >
        <div id="box1" v-if="obj.isShow"></div>
  </el-form-item>
    
         </el-form>
      <span slot="footer">
            <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isAdd" >添加</el-button>
        <el-button type="primary" v-else @click="edit()">编辑 </el-button >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { cateList,goodsAdd,goodsInfo,goodsEdit} from '../../../request/api';
import { warningAlert,sucessAlert } from '../../../utils/alert';
import E from "wangeditor";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
    form:{
        first_cateid:"",
        second_cateid:"",
        goodsname:"",
        price:"",
        market_price:"",
        img:"",
        description:"",
        specsid:"",
        specsattr:"",
        isnew:1,
        ishot:1,
        status:1
        },
    //存放图片地址
    imgUrl:"",
    secondCate:[],
    attrArr:[]
    };
  },
  computed: {
    ...mapGetters({
        CateList:"cate/list",
        SpecsList:"specs/list"
    }),
  },
  methods: {
    ...mapActions({
        CateAsyncList:"cate/acyncChangeList",
        SpecsAsyncList:"specs/asyncList",
         GoodsAsyncList:"goods/asyncList"
    }),
    OneCate(){
        cateList({pid:this.form.first_cateid}).then(res=>{
        // 清空上一次选择的东西
        this.form.second_cateid = ""
        // 给二级分类赋值
        this.secondCate = res.data.list
        })
    },
    changespecs(){
        this.form.specsattr = ""
        let obj = this.SpecsList.find(item=>{
            return this.form.specsid == item.id
        })
        console.log(obj)
        this.attrArr = obj.attrs
    },
    //清空
    empty(){
        this.form={
             first_cateid:"",
        second_cateid:"",
        goodsname:"",
        price:"",
        market_price:"",
        img:"",
        description:"",
        specsid:"",
        specsattr:"",
        isnew:1,
        isshot:1,
        status:1
        },
    this.imgUrl="",
    this.secondCate=[],
    this.attrArr=[]
    },//取消
    cancel(){
        this.obj.isShow =false,
        this.empty()
    },
    //图片思路
    changeimg(e){
        //图片的所有信息
       let file = e.target.files[0]
        //判断类型
       let ImgType = [
             "image/jpeg",
        "image/jpg",
        "image/png",
        "image/webp",
        "image/gif",
       ]

       if(!ImgType.includes(file.type)){
           warningAlert("请上传规定类型的图片")
           return;
       }
        //图片回显
        this.imgUrl = URL.createObjectURL(file)
        //将地址赋给form,编辑的时候需要用到
        this.form.img = file

    },
    //富文本编辑器
    opened(){
        this.editor = new E('#box1')
        this.editor.create()
        this.editor.txt.html(this.form.description)
    },
    // 添加
    add(){
      // this.empty()
      let obj = {
        ...this.form,
        specsattr:JSON.stringify(this.form.specsattr),
        description:this.editor.txt.html()
      }
       goodsAdd(obj).then(res=>{
        if(res.data.code == 200){
          sucessAlert(res.data.msg),
          this.cancel(),
          this.GoodsAsyncList()
          // // 重新请求列表总数
          this.asyncGoodsTotal()
        }else{
          // 失败的提示窗
          warningAlert(res.data.msg)
        }
      })
    },
    getOne(id){
      goodsInfo({id:id}).then(res=>{
          // console.log(res.data.list)
        this.form = {
          ...res.data.list,
          specsattr:JSON.parse(res.data.list.specsattr)
        },
     cateList({pid:this.form.first_cateid}).then(res=>{
          // 给二级分类赋值
          this.secondCate = res.data.list
        }),
        this.imgUrl=this.$pre + this.form.img

        let obj = this.SpecsList.find(item=>{
          return this.form.specsid == item.id
        })
        this.attrArr = obj.attrs
        this.form.id = id
        })
    },
    //编辑
    edit(){
        let obj={
          ...this.form,
           specsattr:JSON.stringify(this.form.specsattr),
           description:this.editor.txt.html()
        }
        console.log(obj)
      goodsEdit(obj).then(res=>{
        console.log(res)
         if(res.data.code == 200){
          // 成功的提示窗
          sucessAlert(res.data.msg)
          // 清空form并关闭遮罩层
          this.cancel()
          // 刷新列表数据
          this.GoodsAsyncList()
          // // 重新请求列表总数
          this.asyncGoodsTotal()
        }else{
          // 失败的提示窗
          warningAlert(res.data.msg)
        }
      })
    },

    },
 mounted() {
      this.CateAsyncList(),
      this.SpecsAsyncList(true),
      this.GoodsAsyncList()
  }
}
</script>
<style scoped>
.box{
    width: 150px;
    height:150px;
    border:1px solid black;
    position: relative;
}
.box i {
    position:absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
}
.box input{
    width: 150px;
    height: 150px;
    opacity: 0;
    position: absolute;
     top:0;
    left: 0;
    z-index: 3;

}
.box img{
    width: 150px;
    height: 150px;
     position: absolute;
     top:0;
    left: 0;

}
</style>